import Vue from 'vue'
import Router from 'vue-router'
import store from '../store/index.js'

Vue.use(Router)

//路由懒加载
const Login = resolve => {
  require.ensure(['@/components/Login.vue'], () => {
    resolve(require('@/components/Login.vue'));
  });
};

const Register = resolve => {
  require.ensure(['@/components/Register.vue'], () => {
    resolve(require('@/components/Register.vue'));
  });
};

const Index = resolve => {
  require.ensure(['@/components/Index.vue'], () => {
    resolve(require('@/components/Index.vue'));
  });
};

//主功能
const Goodlist = resolve => {
  require.ensure(['@/components/Main/Goodlist.vue'], () => {
    resolve(require('@/components/Main/Goodlist.vue'));
  });
};
const addgood = resolve => {
  require.ensure(['@/components/Main/addgood.vue'], () => {
    resolve(require('@/components/Main/addgood.vue'));
  });
};
const rootsetting = resolve => {
  require.ensure(['@/components/setting/rootsetting.vue'], () => {
    resolve(require('@/components/setting/rootsetting.vue'));
  });
};


//个人信息
const UserInfo= resolve => {
  require.ensure(['@/components/userinfo/userinfo.vue'], () => {
    resolve(require('@/components/userinfo/userinfo.vue'));
  });
};
const UpdataPwd= resolve => {
  require.ensure(['@/components/userinfo/updatapwd.vue'], () => {
    resolve(require('@/components/userinfo/updatapwd.vue'));
  });
};

//用户管理
const MemberManagement= resolve => {
  require.ensure(['@/components/member/memberManagement.vue'], () => {
    resolve(require('@/components/member/memberManagement.vue'));
  });
};
const MenuSetting= resolve => {
  require.ensure(['@/components/member/menuSetting.vue'], () => {
    resolve(require('@/components/member/menuSetting.vue'));
  });
};
const OrgSetting= resolve => {
  require.ensure(['@/components/member/orgSetting.vue'], () => {
    resolve(require('@/components/member/orgSetting.vue'));
  });
};
const RoleSetting= resolve => {
  require.ensure(['@/components/member/roleSetting.vue'], () => {
    resolve(require('@/components/member/roleSetting.vue'));
  });
};
const AreaSetting= resolve => {
  require.ensure(['@/components/member/areaSetting.vue'], () => {
    resolve(require('@/components/member/areaSetting.vue'));
  });
};

//项目信息管理
const ProjectType= resolve => {
  require.ensure(['@/components/project/projectType.vue'], () => {
    resolve(require('@/components/project/projectType.vue'));
  });
};

//执收单位管理
const Organization= resolve => {
  require.ensure(['@/components/organization/organization.vue'], () => {
    resolve(require('@/components/organization/organization.vue'));
  });
};

//代收银行
const Bank= resolve => {
  require.ensure(['@/components/bank/bank.vue'], () => {
    resolve(require('@/components/bank/bank.vue'));
  });
};

//缴费项目
const PayItem= resolve => {
  require.ensure(['@/components/payitem/payitem.vue'], () => {
    resolve(require('@/components/payitem/payitem.vue'));
  });
};

//统计管理
const Income= resolve => {
  require.ensure(['@/components/statistics/income.vue'], () => {
    resolve(require('@/components/statistics/income.vue'));
  });
};
const Dao= resolve => {
  require.ensure(['@/components/statistics/dao.vue'], () => {
    resolve(require('@/components/statistics/dao.vue'));
  });
};
const UnDao= resolve => {
  require.ensure(['@/components/statistics/undao.vue'], () => {
    resolve(require('@/components/statistics/undao.vue'));
  });
};

//对账管理
const LiuShui= resolve => {
  require.ensure(['@/components/reconciliation/liushui.vue'], () => {
    resolve(require('@/components/reconciliation/liushui.vue'));
  });
};
const ZiJin= resolve => {
  require.ensure(['@/components/reconciliation/zijin.vue'], () => {
    resolve(require('@/components/reconciliation/zijin.vue'));
  });
};

//分成管理
const Up= resolve => {
  require.ensure(['@/components/divideInto/up.vue'], () => {
    resolve(require('@/components/divideInto/up.vue'));
  });
};
const Down= resolve => {
  require.ensure(['@/components/divideInto/down.vue'], () => {
    resolve(require('@/components/divideInto/down.vue'));
  });
};

//缴库管理
const PayTreasury= resolve => {
  require.ensure(['@/components/payTreasury/payTreasury.vue'], () => {
    resolve(require('@/components/payTreasury/payTreasury.vue'));
  });
};


const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login,
      hidden:true
    },
    {
      path: '/register',
      name: 'register',
      component: Register,
      hidden:true
    },
    // {
    //   path: '/addgood/:id',
    //   name: 'addgood',
    //   component: addgood,
    //   hidden:true
    // },
    {
      path: '/',
      name: '个人信息',
      component: Index,
      meta: {
        requiresAuth: false
      },
      redirect: '/userinfo',
      iconCls: 'iconfont icon-yonghuguanli',
      ismine:'userinfo',
      hidden:false,
      istwo:true,
      children: [
        { path: '/userinfo', component: UserInfo, name: '个人信息',iconCls: 'iconfont icon-yonghuguanli',ismine:'userinfo'},
        { path: '/updatapwd', component: UpdataPwd, name: '修改密码', iconCls:'iconfont icon-zuzhijigou',ismine:'userinfo' }
      ]
    },
    {
      path: '/',
      name: '用户管理',
      component: Index,
      meta: {
        requiresAuth: false
      },
      redirect: '/memberManagement',
      iconCls: 'iconfont icon-yonghuguanli',
      ismine:'user',
      hidden:false,
      istwo:true,
      children: [
        { path: '/memberManagement', component: MemberManagement, name: '用户管理',iconCls: 'iconfont icon-yonghuguanli',ismine:'user'},
        { path: '/orgsetting', component: OrgSetting, name: '机构管理', iconCls:'iconfont icon-zuzhijigou',ismine:'user' },
        { path: '/rolesetting', component: RoleSetting, name: '角色管理',iconCls:'iconfont icon-daren',ismine:'user'},
        { path: '/areasetting', component: AreaSetting, name: '区域管理',iconCls:'iconfont icon-diqu' ,ismine:'user'},
        { path: '/menusetting', component: MenuSetting, name: '菜单管理', iconCls:'iconfont icon-iconset0194',ismine:'user' },
      ]
    },
    {
      path: '/',
      component: Index,
      name: '项目信息管理',
      meta: {
        requiresAuth: false
      },
      redirect: '/project',
      iconCls: 'iconfont icon-xitongshezhi',
      ismine:'project',
      hidden:false,
      istwo:false,
      children: [
          { path: '/project', component: ProjectType, name: '项目类型', iconCls:'iconfont icon-zuzhijigou',ismine:'project' }
      ]
    },
    {
      path: '/',
      component: Index,
      name: '执收单位管理',
      meta: {
        requiresAuth: false
      },
      redirect: '/organization',
      iconCls: 'iconfont icon-xitongshezhi',
      ismine:'organization',
      hidden:false,
      istwo:false,
      children: [
          { path: '/organization', component: Organization, name: '执收单位', iconCls:'iconfont icon-zuzhijigou',ismine:'organization' }
      ]
    },
    {
      path: '/',
      component: Index,
      name: '代收银行',
      meta: {
        requiresAuth: false
      },
      redirect: '/bank',
      iconCls: 'iconfont icon-xitongshezhi',
      ismine:'bank',
      hidden:false,
      istwo:false,
      children: [
          { path: '/bank', component: Bank, name: '代收银行', iconCls:'iconfont icon-zuzhijigou',ismine:'bank' }
      ]
    },
    {
      path: '/',
      component: Index,
      name: '缴费项目关联',
      meta: {
        requiresAuth: false
      },
      redirect: '/payitem',
      iconCls: 'iconfont icon-xitongshezhi',
      ismine:'payitem',
      hidden:false,
      istwo:false,
      children: [
          { path: '/payitem', component: PayItem, name: '缴费项目关联', iconCls:'iconfont icon-zuzhijigou',ismine:'payitem' }
      ]
    },
    {
      path: '/',
      component: Index,
      name: '统计管理',
      meta: {
        requiresAuth: false
      },
      redirect: '/income',
      iconCls: 'iconfont icon-xitongshezhi',
      ismine:'statistics',
      hidden:false,
      istwo:true,
      children: [
          { path: '/income', component: Income, name: '收入查询', iconCls:'iconfont icon-zuzhijigou',ismine:'statistics' },
          { path: '/dao', component: Dao, name: '已到账查询', iconCls:'iconfont icon-zuzhijigou',ismine:'statistics' },
          { path: '/undao', component: UnDao, name: '未到账查询', iconCls:'iconfont icon-zuzhijigou',ismine:'statistics' }
      ]
    },
    {
      path: '/',
      component: Index,
      name: '对账管理',
      meta: {
        requiresAuth: false
      },
      redirect: '/liushui',
      iconCls: 'iconfont icon-xitongshezhi',
      ismine:'reconciliation',
      hidden:false,
      istwo:true,
      children: [
          { path: '/liushui', component: LiuShui, name: '交易流水对账', iconCls:'iconfont icon-zuzhijigou',ismine:'reconciliation' },
          { path: '/zijin', component: ZiJin, name: '账户资金对账', iconCls:'iconfont icon-zuzhijigou',ismine:'reconciliation' }
      ]
    },
    {
      path: '/',
      component: Index,
      name: '分成管理',
      meta: {
        requiresAuth: false
      },
      redirect: '/up',
      iconCls: 'iconfont icon-xitongshezhi',
      ismine:'divide_into',
      hidden:false,
      istwo:true,
      children: [
          { path: '/up', component: Up, name: '向上级财政', iconCls:'iconfont icon-zuzhijigou',ismine:'divide_into' },
          { path: '/down', component: Down, name: '向下级财政', iconCls:'iconfont icon-zuzhijigou',ismine:'divide_into' }
      ]
    },
    {
      path: '/',
      component: Index,
      name: '缴库管理',
      meta: {
        requiresAuth: false
      },
      redirect: '/pay_treasury',
      iconCls: 'iconfont icon-xitongshezhi',
      ismine:'pay_treasury',
      hidden:false,
      istwo:false,
      children: [
          { path: '/pay_treasury', component: PayTreasury, name: '缴库管理', iconCls:'iconfont icon-zuzhijigou',ismine:'pay_treasury' }
      ]
    },
  ]
});


//注册全局钩子用来拦截导航
router.beforeEach((to, from, next) => {
  //判断要去的路由有没有requiresAuth
  if (to.meta.requiresAuth) {
    if(localStorage.getItem('userlogin')){
      next();
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }  // 将刚刚要去的路由path（却无权限）作为参数，方便登录成功后直接跳转到该路由
      });
    }

  } else {
    next();//如果无需token,那么随它去吧
  }
});
export default router
